package com.chenchao.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/25 11:14
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.net.Test06C.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * UDP服务端（也可以是客户端）
 */
public class Test06S {
    public static void main(String[] args) throws IOException {
        /*新建端口*/
        DatagramSocket datagramSocket = new DatagramSocket(8080);

        /*接收包裹,会一直监听，直到收到包裹*/
        DatagramPacket datagramPacket = new DatagramPacket(new byte[1024], 1024);
        datagramSocket.receive(datagramPacket);

        /*打开包裹,可以查看发送者信息*/
        System.out.println(datagramPacket.getAddress());
        System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
        System.out.println(datagramPacket.getPort());
        /*关闭资源*/
        datagramSocket.close();
    }
}
